#!/usr/bin/env python
# encoding: utf-8
# author: 04
"""
This is a copyed code from
https://stackoverflow.com/questions/29126989/django-making-a-list-of-a-field-grouping-by-another-field-in-model/29128127
origin is https://gist.github.com/ludoo/ca6ed07e5c8017272701
---- update ----
copyed func is too old.
```
AttributeError: module 'django.db.models.sql' has no attribute 'aggregates'
```
read new format from
https://docs.djangoproject.com/en/2.0/ref/models/expressions/#django.db.models.F
and give a fast read for GROUP_CONCAT
https://www.cnblogs.com/zhming26/p/6382995.html
"""

from django.db import models


class GroupConcat(models.Aggregate):

    function = 'GROUP_CONCAT'

    @property
    def template(self):
        if self.extra.get('separator'):
            return '%(function)s(%(field)s SEPARATOR "%(separator)s")'
        else:
            return '%(function)s(%(field)s)'
